package com.teaching;

import cn.hutool.core.io.unit.DataSizeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.MultipartConfigElement;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Administrator
 */
@SpringBootApplication
@Slf4j
public class TeachingHelperApplication extends SpringBootServletInitializer {


    /**
     * web容器中进行部署,继承SpringBootServletInitializer类，重写父类的configure方法
     *
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(TeachingHelperApplication.class);
    }

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(TeachingHelperApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        path = StringUtils.isEmpty(path) ? "" : path;
        log.info("\n----------------------------------------------------------\n\t"
                + "Application  is running! Access URLs:\n\t"
                + "Local: \t\thttp://localhost:" + port + path + "/\n\t"
                + "External: \thttp://" + ip + ":" + port + path + "/\n\t"
                + "swagger-ui: http://" + ip + ":" + port + path + "/swagger-ui.html\n\t"
                + "Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n"
                + "----------------------------------------------------------");
    }
	@Bean
	public CorsFilter corsFilter() {
		final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration corsConfiguration = new CorsConfiguration();
		/*是否允许请求带有验证信息*/
		corsConfiguration.setAllowCredentials(true);
		/*允许访问的客户端域名*/
		corsConfiguration.addAllowedOriginPattern("*");
		/*允许服务端访问的客户端请求头*/
		corsConfiguration.addAllowedHeader("*");
		/*允许访问的方法名,GET POST等*/
		corsConfiguration.addAllowedMethod("*");
		urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
		return new CorsFilter(urlBasedCorsConfigurationSource);
	}

	@Value("${spring.http.mult1ipart.max-file-size}")
	private String maxfilesize;

	@Value("${spring.http.mult1ipart.max-request-size}")
	private String maxrequestsize;

	/**
	 * 文件上传配置
	 *
	 * @return
	 */
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//  单个数据大小
		factory.setMaxFileSize(DataSize.of(DataSizeUtil.parse(maxfilesize), DataUnit.MEGABYTES));
		/// 总上传数据大小
		factory.setMaxFileSize(DataSize.of(DataSizeUtil.parse(maxrequestsize), DataUnit.MEGABYTES));
		return factory.createMultipartConfig();
	}

}
